# -*- coding: utf-8 -*-
"""
@Time    : 2024/10/10 13:52 
@Author  : ZhangShenao 
@File    : 4.保护临界资源.py 
@Desc    : 使用锁保护临界资源
"""

from account import Account
from recharge_task import RechargeTask

if __name__ == '__main__':
    # 模拟转账操作

    # 创建账户
    account = Account(balance=0)

    threads: list[RechargeTask] = []

    # 模拟100个线程并发转账
    for i in range(100):
        task = RechargeTask(account=account, amount=1)
        threads.append(task)
        task.start()

    # 等待线程全部执行完成
    for thread in threads:
        thread.join()

    # 打印账户余额
    print(f"充值完成,当前账号余额: {account.balance}")
